if (!window.ctp_trip_plan_v1)
	window.ctp_trip_plan_v1 = {};
	

ctp_trip_plan_v1.Page = function() 
{
}

ctp_trip_plan_v1.Page.prototype =
{
	handleLoad: function(control, userContext, rootElement) 
	{
		this.control = control;
		
		// Sample event hookup:	
		rootElement.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleMouseDown));
	},
	
	// Sample event handler
	handleMouseDown: function(sender, eventArgs) 
	{
		// The following line of code shows how to find an element by name and call a method on it.
		// this.control.content.findName("Timeline1").Begin();
	}
}


//save the sender
var sl;


// for drag&drop
var beginX;
var beginY;
var isMouseDown = false;
var dragimgname="";
var dragimgname1="";

// for insert_anmation
  var startpos=187;
  var nextpos=110;
  
  var startposy=8;
  var nextposy=135;
  
  var insertanstatus=0;
  var startobj=0;
  var startobjvalue=0;
  var startr1="";
  var startr2="";
  var moveobj,moveobjno;
  
  var itemlist=new Array(100);
  var old_itemlist=new Array(100);
  
  var item_r1=new Array(100);
  var item_r2=new Array(100);
  
  for( i=0;i<100;i++)
  {
   itemlist[i]=-1;
  }
  
  //itemlist[0]=0;
  //itemlist[1]=1;
  //itemlist[2]=2;
  //itemlist[3]=3;
  //itemlist[4]=-1;
  var tempx;
  var ismoving=0
  
  var item_amount=0;
  
  var drag_y_index=0;
  


function objectclick(sender,mouseEventArgs)
{


    
    beginX = mouseEventArgs.getPosition(null).x;
    beginY = mouseEventArgs.getPosition(null).y;

    isMouseDown = true;

    // Ensure this object is the only one receiving mouse events.
    sender.captureMouse();

    
    sender.SetValue("Opacity",0.5);
    sender["Canvas.ZIndex"]=99;
    
    /*
    if(sender["Canvas.Top"]>startposy+100)
    {
       startobj=-1;
       startobjvalue=itemlist[4];
       itemlist[4]=-1;
    }*/


      // var existinlist=false;
  
       for(i=0;i<item_amount;i++)
       {
      
         if(sender["name"]=="o"+itemlist[i])
         {  
             startobj=i;
             startobjvalue=itemlist[i];
             startr1=item_r1[i];
             startr2=item_r2[i];
             
            // existinlist=true;
          
         }
      
      
       }
       
       
       drag_y_index= parseInt( (beginY-startposy)/nextposy );
       

      // document.all["bt1"].value=drag_y_index;
    
    /*
       if(existinlist==false)
       {
          sender.SetValue("source","o5.png");
          startobj=-1;
          startobjvalue=4;
       }
    */
     

}

function objectrelease(sender,args)
{

   // sender.findname("boy").SetValue("source","tryito/boy.png");
   // sender.SetValue("source",dragimgname);


    isMouseDown = false;

    // Allow all objects to receive mouse events.
    sender.releaseMouseCapture();
    
    
/*    
    if(sender["Canvas.Left"]<(startpos-40))
    {
    
       if(itemlist[4]!=-1)
       {
        // alert(startobj);
          for(i=startobj;i<4;i++) itemlist[i]=itemlist[i+1];
                   itemlist[4]=-1;
                   
          sender.SetValue("Visibility","Collapsed");


       }
    
    }*/

    
    redrawthis(sender,args);
    

    
    sender.SetValue("Opacity",1);
    sender["Canvas.ZIndex"]=1;
    
    



}

function objectmove(sender, mouseEventArgs)
{


    // Determine whether the mouse button is down.
    // If so, move the object.
    
    //document.all["ytext"].innerText=sender.findname("o"+startobj)["Canvas.Left"]+",start:"+(startpos-10)+",end:"+ (startpos+10);
  // document.all["ytext"].innerText=itemlist[0]+","+itemlist[1]+","+itemlist[2]+","+itemlist[3]+",start:"+startobj+","+itemlist[4];
    
    if (isMouseDown == true)
    {
    
    //stop user drag items out of range, didn't work yet.
    /*
    if(sender["Canvas.Left"]<9)
       {
           sender["Canvas.Left"]=9;
       }
       else if(sender["Canvas.Left"]+sender["Width"]>601)
       {
           sender["Canvas.Left"] = 600-sender["Width"];
       }
       
       
       if(sender["Canvas.Top"]<38)
       {
           sender["Canvas.Top"]=38;w
       }
       else if(sender["Canvas.Top"]+sender["Height"]>367)
       {
           sender["Canvas.Top"] = 360-sender["Height"];
       }
    */
    
    
    
        // Retrieve the current position of the mouse.
        var currX = mouseEventArgs.getPosition(null).x;
        var currY = mouseEventArgs.getPosition(null).y;

        // Reset the location of the object.
        sender["Canvas.Left"] += currX - beginX;
        sender["Canvas.Top"] += currY - beginY;

        // Update the beginning position of the mouse.
        beginX = currX;
        beginY = currY;
        
  
        if(ismoving==0)
        {
           now_y_index= parseInt( (currY-startposy)/nextposy );
           
          // document.all["bt1"].value="move"+now_y_index;
           //alert(item_amount);
           if(now_y_index!=drag_y_index)
           {
           
                drag_y_index=now_y_index;

                if(startobj==-1)
                {
                   return;
                }
                
           
                for(k=startobj;k<item_amount;k++)
                {
                   itemlist[k]=itemlist[k+1];
                   
                   item_r1[k]=item_r1[k+1];
                   item_r2[k]=item_r2[k+1];
                   
                }
                //k[item_amount-1]=-1;
                item_amount--;
                
                redrawthis();
               
                startobj=-1;
                
                
                //alert(item_amount);
                return;
           }

        
          for(i=0;i<4;i++)
          {
          
          
           nowpos=startpos+nextpos*i;
           if(sender["Canvas.Left"]>nowpos-30 && sender["Canvas.Left"]<nowpos+30 && i!=startobj && itemlist[i+now_y_index*4]!=-1)
           {

              if(startobj==-1)
              {
              //if(now_y_index!=drag_y_index)
              
                for(k=i;k<4;k++)
                 {
                    if(itemlist[k+now_y_index*4]!=-1)
                    {
                    
                    //alert(k+now_y_index*4);
                       moveobj=itemlist[k+now_y_index*4];
                       moveobjno=k+now_y_index*4;
                       insertan_back(sender,mouseEventArgs);
                    }
                 }
                 
                 
                 
                // for(k=4;k>i;k--)
                 for(k=item_amount;k>i+now_y_index*4;k--)
                 {
                      itemlist[k]=itemlist[k-1];
                      
                      item_r1[k]=item_r1[k-1];
                      item_r2[k]=item_r2[k-1];
                     
                 }
                 
                // item_r1=item_r1[startobj];
                             
                 itemlist[i+now_y_index*4]=startobjvalue;
                 startobj=i+now_y_index*4;
                 
                 item_r1[i+now_y_index*4]=startr1;
                 item_r2[i+now_y_index*4]=startr2;
                 
                 item_amount++;

                 
              
              }
              else
              {                   
              
                 //document.all["bt1"].value=i+drag_y_index*4;
                                 
                 moveobj=itemlist[i+drag_y_index*4];
                 moveobjno=i+drag_y_index*4;

                 if(startobj%4<i)
                 {
                    ismoving=1;
                    insertan_forward(sender,mouseEventArgs);
                 }
                 if(startobj%4>i) 
                 {
                    ismoving=1;
                    insertan_back(sender,mouseEventArgs);
                 }
              
                 olditem=itemlist[i+drag_y_index*4];
                 itemlist[i+drag_y_index*4]=itemlist[startobj];
                 itemlist[startobj]=olditem;
                 
                 oldr1=item_r1[i+drag_y_index*4];
                 item_r1[i+drag_y_index*4]=item_r1[startobj];
                 item_r1[startobj]=oldr1;
                 
                 oldr2=item_r2[i+drag_y_index*4];
                 item_r2[i+drag_y_index*4]=item_r2[startobj];
                 item_r2[startobj]=oldr2;
              
                 startobj=i+drag_y_index*4;
              
              }
           }
        
          }
        
        }


        
        
    }



}










function insertan_back(sender,args)
{

var i;
//insertanstatus=0;

 i=moveobj;
 //document.all["bt1"].value="back"+moveobjno;
 
// document.all["mymsg"].innerText+=i+",";
 
    aa=sender.findname("Timeline"+moveobjno%4).Children.getItem(0);

otarget=aa["StoryBoard.TargetName"];

if(otarget!="o999")
{

   tempx=sender.findname(otarget)["Canvas.Left"];   
   sender.findname("Timeline"+moveobjno%4).stop();
   sender.findname(otarget)["Canvas.Left"]=tempx;
}
 
 //  tempx=sender.findname("o"+i)["Canvas.Left"];   
 // sender.findname("Timeline"+i%4).stop();
 //  sender.findname("o"+i)["Canvas.Left"]=tempx;



   //aa=sender.findname("Timeline"+i%4).Children.getItem(0);
   bb=aa.KeyFrames.getItem(0);
   bb.Value=sender.findname("o"+i)["Canvas.Left"];
   
   cc=aa.KeyFrames.getItem(1);
   cc.Value=sender.findname("o"+i)["Canvas.Left"]+nextpos;
   
   aa["StoryBoard.TargetName"]="o"+i;
   sender.findname("Timeline"+moveobjno%4).begin();
   

 

   
}

function insertan_forward(sender,args)
{

var i;
//insertanstatus=1;


 
i=moveobj;

 //document.all["bt1"].value="forward"+i;

   aa=sender.findname("Timeline"+moveobjno%4).Children.getItem(0);

otarget=aa["StoryBoard.TargetName"];


if(otarget!="o999")
{
   tempx=sender.findname(otarget)["Canvas.Left"];   
   sender.findname("Timeline"+moveobjno%4).stop();
   sender.findname(otarget)["Canvas.Left"]=tempx;

}
   
   cc=aa.KeyFrames.getItem(0);
   cc.Value=sender.findname("o"+i)["Canvas.Left"];
   
   bb=aa.KeyFrames.getItem(1);
   bb.Value=sender.findname("o"+i)["Canvas.Left"]-nextpos;
   
   
   aa["StoryBoard.TargetName"]="o"+i;
   sender.findname("Timeline"+moveobjno%4).begin();
   
    //sender.findname("o"+i).SetValue("Canvas.Left",0);

   

   
 
   
}


function insertanover(sender,args)
{

  ismoving=0;
  /*
  aa=sender.Children.getItem(0);
  bb=aa["StoryBoard.TargetName"];
document.all["bt1"].value="over:"+bb;  
   tempx=sender.findname(bb)["Canvas.Left"];   
   sender.stop();
   sender.findname(bb)["Canvas.Left"]=tempx;
*/

}


function redrawthis()
{

   var nowno=0;

   for(i=0;i<4;i++)
   {
      for(j=0;j<4;j++)
      {   
         nowno=i*4+j;
         

         
         if(itemlist[nowno]!=-1)
         {
         
                 sl.findname("a"+nowno).SetValue("Source","short.png");


         
                 sl.findname("o"+itemlist[nowno]).SetValue("Canvas.Left",startpos+nextpos*j);
                 sl.findname("o"+itemlist[nowno]).SetValue("Canvas.Top",startposy+nextposy*i);

              
                 var r2=item_r2[nowno];
                 
                 
                 
                 if(r2!="")
                 {
                   var r2_a=r2.split(",");
                 
                   //check if next item is near
                   for(k=0;k<r2_a.length;k++)
                   {
                      if(itemlist[nowno+1]!="-1")
                      {
                         if(r2_a[k]==itemlist[nowno+1])
                         {
                         
                          sl.findname("a"+nowno).SetValue("Source","long.png");

                          }
                       
                       }  
                 
                     }
                 }
                 
                    var scaleTransform = sl.findName("s"+itemlist[nowno]);
                    if(scaleTransform.GetValue("ScaleX")!=1)
                    {
                      scaleTransform.ScaleX = 1;
                      scaleTransform.ScaleY = 1;
                    }
                 
                 
                    //check if this item is included in previous item
                    if(nowno!=0)
                    {
                   
                       var r1=item_r1[nowno-1];
                     
                       if(r1!="")
                       {
                         var r1_a=r1.split(",");
                 
               

                 
                         for(k=0;k<r1_a.length;k++)
                         {
                       
                           if(r1_a[k]==itemlist[nowno])
                           {
                         
                             var scaleTransform = sl.findName("s"+itemlist[nowno]);
                             
                             if(scaleTransform.GetValue("ScaleX")==1)
                             {

                             // Increase ScaleX and ScaleY by 25%.
                                scaleTransform.ScaleX = 0.9;
                                scaleTransform.ScaleY = 0.9;
                             
                            }

                             sl.findname("o"+itemlist[nowno]).SetValue("Canvas.Left",startpos+nextpos*j+8);
                             sl.findname("o"+itemlist[nowno]).SetValue("Canvas.Top",startposy+nextposy*i+8);
                           
                             //alert("a"+(nowno-1));
                             sl.findname("a"+(nowno-1)).SetValue("Source","l_brace.png");
                             sl.findname("a"+nowno).SetValue("Source","r_brace.png");



                            }
                         }
                 
                 
                      }  
                 
                 }
                 
                 
                 
                 

          
         }      
         
         
      }
   }
   
  
  
  //insertanstatus=0;


}


function insert_new_node1(fn,no,r1,r2,title)
{

//alert(r1);
checkexist=false;
for(i=0;i<item_amount;i++)
{
  if(itemlist[i]==no) checkexist=true;
}

if(checkexist)
{
//alert("this item is in your plan alreay");
return;
}


   var control = sl.getHost();

   // var xamlFragment = "<Canvas  x:Name=\"o"+no+"\" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" Width=\"90\" Height=\"110\" Canvas.Top=\"121\" Canvas.Left=\"182\"  MouseMove=\"objectmove\" MouseLeftButtonDown=\"objectclick\" MouseLeftButtonUp=\"objectrelease\"><Image Width=\"91\" Height=\"110\" Canvas.Left=\"0\" Canvas.Top=\"0\" Source=\"back.png\" Stretch=\"Fill\"/><Image Width=\"75\" Height=\"75\"   Canvas.Top=\"6\" Source=\""+fn+"\" Stretch=\"Fill\" RenderTransformOrigin=\"0.013,0.227\" Canvas.Left=\"7\"/><TextBlock Width=\"82\" Height=\"17\"  Canvas.Left=\"7\" Canvas.Top=\"85\" TextWrapping=\"Wrap\" FontFamily=\"Verdana\" FontSize=\"10\" TextDecorations=\"Underline\"><Run Text=\"The Peak\"/></TextBlock></Canvas>";
 var xamlFragment = "<Canvas  x:Name=\"o"+no+"\" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" Width=\"90\" Height=\"110\" Canvas.Top=\"121\" Canvas.Left=\"182\"  MouseMove=\"objectmove\" MouseLeftButtonDown=\"objectclick\" MouseLeftButtonUp=\"objectrelease\"><Image Width=\"91\" Height=\"110\" Canvas.Left=\"0\" Canvas.Top=\"0\" Source=\"back.png\" Stretch=\"Fill\"/><Image Width=\"75\" Height=\"75\"   Canvas.Top=\"6\" Source=\""+fn+"\" Stretch=\"Fill\" RenderTransformOrigin=\"0.013,0.227\" Canvas.Left=\"7\"/><TextBlock Width=\"82\" Height=\"17\"  Canvas.Left=\"7\" Canvas.Top=\"85\" TextWrapping=\"Wrap\" FontFamily=\"Verdana\" FontSize=\"10\" TextDecorations=\"Underline\"><Run Text=\""+title+"\"/></TextBlock>";
 
 xamlFragment+='<Canvas.RenderTransform><ScaleTransform xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="s'+no+'" /></Canvas.RenderTransform>';
 xamlFragment+="</Canvas>";




    var rectangle_4 = control.content.createFromXaml(xamlFragment);
    sl.children.insert(0, rectangle_4);
    
    
    //insert arrow
        var xamlFragment1 = '<Image Canvas.ZIndex="30" x:Name="a'+item_amount+'" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="82" Height="88" Canvas.Left="0" Canvas.Top="0" Source="short.png" Stretch="Fill"/>';

    var rectangle_1 = control.content.createFromXaml(xamlFragment1);
    sl.children.insert(0, rectangle_1);
    
    a_x=item_amount%4;
    a_y=parseInt(item_amount/4);
    
    sl.findname("a"+item_amount).SetValue("Canvas.Left",63+startpos+nextpos*a_x);
    sl.findname("a"+item_amount).SetValue("Canvas.Top",10+startposy+nextposy*a_y);
      
    //insert delete_button 
    var xamlFragment2 = '<Image Canvas.ZIndex="30" x:Name="d'+item_amount+'" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="20" Height="19" Canvas.Left="0" Canvas.Top="0" Source="del.png" Stretch="Fill" MouseLeftButtonUp="deletenode" />';

    var rectangle_2 = control.content.createFromXaml(xamlFragment2);
    sl.children.insert(0, rectangle_2);
        
    sl.findname("d"+item_amount).SetValue("Canvas.Left",34+startpos+nextpos*a_x);
    sl.findname("d"+item_amount).SetValue("Canvas.Top",100+startposy+nextposy*a_y);      
    
    
    //determine where to insert
    var near=-1;
    
    for(i=0;i<item_amount;i++)
    {
       var m_r1=item_r1[i];
       
       if(m_r1!="")
       {
       //alert(i+":"+item_r1[i]);
          var r1_a=m_r1.split(",");
          
          for(j=0;j<r1_a.length;j++)
          {
            if(r1_a[j]==no)
            {
               near=i;
            }
       
          }
          
          
        }    
        
        var m_r2=item_r2[i];
        
       if(m_r2!="")
       {
          var r2_a=m_r2.split(",");
          
          for(j=0;j<r2_a.length;j++)
          {
            if(r2_a[j]==no)
            {
               near=i;
            }
       
          }
          
          
        }    

    }
    
    
    if(near==-1)
    {
       itemlist[item_amount]=no;
       item_r1[item_amount]=r1;
       item_r2[item_amount]=r2;
    }
    else
    {
        
                 for(k=item_amount;k>near+1;k--)
                 {
                      itemlist[k]=itemlist[k-1];
                      
                      item_r1[k]=item_r1[k-1];
                      item_r2[k]=item_r2[k-1];
                     
                 }
  
                             
                 itemlist[near+1]=no;
          
                 
                 item_r1[near+1]=r1;
                 item_r2[near+1]=r2;
                 
    }
    
    
    
    item_amount++;
    
    redrawthis();
    
   // alert("playanbb");
    
   aa=sl.findname("newnode").Children.getItem(0);
   sl.findname("newnode").stop();
   
  // alert( aa);


   aa["StoryBoard.TargetName"]="o"+no;
   sl.findname("newnode").begin();
   
//alert("cc");

}




//save the sender object when silverlight is loaded
function myinit(sender, mouseEventArgs)
{
sl=sender;
  redrawthis();


}

function deletenode(sender,mouseEventArgs)
{
var sendername=sender.Name;
var delno=parseInt(sendername.substring(1,sendername.length));           

 sl.children.remove( sl.findname("o"+itemlist[delno]) );
 sl.children.remove( sl.findname("a"+(item_amount-1) ) );
 sl.children.remove( sl.findname("d"+(item_amount-1) ) );




     for(k=delno;k<item_amount;k++)
     {
                   itemlist[k]=itemlist[k+1];
                   
                   item_r1[k]=item_r1[k+1];
                   item_r2[k]=item_r2[k+1];
                   
      }


   
   
item_amount=item_amount-1;

redrawthis();

checknear();

}

var dataitemstring="";

function checknear()
{

dataitem_a=dataitemstring.split(",");


                      for(a=0;a<dataitem_a.length;a++)
                      {
                           //alert(a+","+dataitem_a[a]);
                         document.all["near"+dataitem_a[a]].style.display="none";
  
                      }
                       
                       

        for(i=0;i<item_amount;i++)
        {
                 var r2=item_r2[i];
                 
                 
                 
                 if(r2!="")
                 {
                   var r2_a=r2.split(",");
                 
                   //check if next item is near
                   for(k=0;k<r2_a.length;k++)
                   {
                      for(j=0;j<dataitem_a.length;j++)
                      {
                       
                         if(r2_a[k]==dataitem_a[j])
                         {
                            
                            document.all["near"+dataitem_a[j]].style.display="inline";

                          }
                       }
                       
                 
                     }
                 }
                 
           }
 
}